﻿using System;
using System.Collections.Generic;
using System.Text;
using MobileBroadbandLoggingMonitorGUI.UI.Interfaces;
using MobileBroadbandLoggingMonitorGUI.Interfaces;
using MobileBroadbandLoggingMonitorUI.UI.Interfaces;
using System.Windows.Forms;

namespace MobileBroadbandLoggingMonitorGUI.UI
{
    public class ShareInformationPresenter : IShareInformationPresenter
    {
        private IShareInformationView _shareInformationView;
        private string _informationToBeShared = string.Empty;
        private bool _initialized;
        private IServerCommunication _serverCommunication;
        private IDialogPresenter _dialogPresenter;


        public ShareInformationPresenter(IShareInformationView shareInformationView, IServerCommunication serverCommunication, IDialogPresenter dialogPresenter)
        {
            _shareInformationView = shareInformationView;
            _serverCommunication = serverCommunication;
            _dialogPresenter = dialogPresenter;
        }

        public override void Show(string informationToBeShared)
        {
            _informationToBeShared = informationToBeShared;
            InitializeViewAndPresenter();
            _shareInformationView.InformationToBeShared = informationToBeShared;
            _shareInformationView.ShowDialog();
        }

        private void InitializeViewAndPresenter()
        {
            if (!_initialized)
            {
                _shareInformationView.ShareClicked += new EventHandler(_shareInformationView_ShareClicked);
                _shareInformationView.CancelClicked += new EventHandler(_shareInformationView_CancelClicked);
                _initialized = true;
            }
        }

        void _shareInformationView_CancelClicked(object sender, EventArgs e)
        {
            _shareInformationView.Close();
        }

        void _shareInformationView_ShareClicked(object sender, EventArgs e)
        {
            if (SendInformationToServer())
            {
                _shareInformationView.Close();
            }

        }

        private bool SendInformationToServer()
        {
            try
            {
                _serverCommunication.SendSharedInformationToServer(
                    _shareInformationView.MobileBroadbandCarrierName,
                    _shareInformationView.MobileSoftwareName,
                    _informationToBeShared,
                    _shareInformationView.Email
                    );
                return true;
            }
            catch (Exception ex)
            {
                _dialogPresenter.ShowDialog(
                    "Your information was not sent to the server. This might be because you're temporarily off-line or that the server could not be reached.\n\nPlease try again.",
                    "Oops! An error occured..."
                    , MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }
        }

   
    }
}
